/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.usuallyinterface;

import com.lightkits.common.TransactionValidate;
import com.lightkits.common.db.entity.FndWmMoveTypeBase;
import com.lightkits.framework.common.logging.Logger;
import com.lightkits.framework.common.logging.LoggerFactory;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.common.pipeline.session.SessionInfo;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.database.dao.WmsFndWmMoveTypeBaseMapper;
import com.lightkits.wms.database.model.FndWmMoveTypeModel;
import com.lightkits.wms.dto.usuallyinterface.GetWmMoveTypeRequestType;
import com.lightkits.wms.dto.usuallyinterface.GetWmMoveTypeResponseType;
import com.lightkits.wms.dto.usuallyinterface.model.WmMoveTypeDTO;
import com.lightkits.wms.util.DomainUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取移动类型详情
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/4/18 14:54
 */
@Component
public class GetWmMoveTypeExecutor implements ServiceExecutor<GetWmMoveTypeRequestType, GetWmMoveTypeResponseType> {
    private static final Logger LOGGER = LoggerFactory.getLogger(GetWmMoveTypeExecutor.class);

    private final WmsFndWmMoveTypeBaseMapper fndWmMoveTypeBaseMapper;

    @Autowired
    public GetWmMoveTypeExecutor(WmsFndWmMoveTypeBaseMapper fndWmMoveTypeBaseMapper) {
        this.fndWmMoveTypeBaseMapper = fndWmMoveTypeBaseMapper;
    }

    @Override
    public GetWmMoveTypeResponseType service(GetWmMoveTypeRequestType request) {
        GetWmMoveTypeResponseType response = new GetWmMoveTypeResponseType();
        response.setSuccessful(true);
        try {
            //参数有效性校验
            TransactionValidate check1 = TransactionValidate.beginValidity(FndWmMoveTypeBase.class);
            check1.checkIdValidity(ObjectUtil.toString(request.getMoveTypeId()), "ID")
                    .checkIdValidity(ObjectUtil.toString(request.getMoveTypeCode()), "MOVE_TYPE_CODE");
            //获取移动类型详情
            List<FndWmMoveTypeModel> moveTypeModels =
                    fndWmMoveTypeBaseMapper.queryWmMoverTypeById(
                            request.getMoveTypeId(), request.getMoveTypeCode(), SessionInfo.languageCode()
                    );
            if (ObjectUtil.isNullOrEmpty(moveTypeModels)) {
                response.setWmMoveTypeDTOS(new ArrayList<>());
                return response;
            }
            response.setWmMoveTypeDTOS(convertDto(moveTypeModels));
        } catch (Exception e) {
            response.setSuccessful(false);
            response.setMessage("get WM Move Type error");
        }
        return response;
    }

    /**
     * 转换类型
     *
     * @param models 转换参数
     * @return WmMoveTypeDTO
     */
    private List<WmMoveTypeDTO> convertDto(List<FndWmMoveTypeModel> models) {
        List<WmMoveTypeDTO> dtos = new ArrayList<>();
        try {
            models.forEach(target -> {
                WmMoveTypeDTO wmMoveTypeDTO = new WmMoveTypeDTO();
                wmMoveTypeDTO.setWmMoveTypeId(target.getWmMoveTypeId());
                wmMoveTypeDTO.setWmMoveType(target.getWmMoveType());
                wmMoveTypeDTO.setWmMoveTypeName(target.getWmMoveTypeName());
                wmMoveTypeDTO.setWmMoveTypeCode(target.getWmMoveTypeCode());
                wmMoveTypeDTO.setWmMoveTypeDesc(target.getWmMoveTypeDesc());
                wmMoveTypeDTO.setFromWarehouseId(target.getFromWarehouseId());
                wmMoveTypeDTO.setFromWarehouseCode(target.getFromWarehouseCode());
                wmMoveTypeDTO.setFromWarehouseName(target.getFromWarehouseName());
                wmMoveTypeDTO.setFromWmAreaFlag(DomainUtil.convertFlag(target.getFromWmAreaFlag()));
                wmMoveTypeDTO.setFromWmAreaId(target.getFromWmAreaId());
                wmMoveTypeDTO.setFromWmAreaCode(target.getFromWmAreaCode());
                wmMoveTypeDTO.setFromWmAreaName(target.getFromWmAreaName());
                wmMoveTypeDTO.setToWarehouseId(target.getToWarehouseId());
                wmMoveTypeDTO.setToWarehouseCode(target.getToWarehouseCode());
                wmMoveTypeDTO.setToWarehouseName(target.getToWarehouseName());
                wmMoveTypeDTO.setToWmAreaFlag(DomainUtil.convertFlag(target.getToWmAreaFlag()));
                wmMoveTypeDTO.setToWmAreaId(target.getToWmAreaId());
                wmMoveTypeDTO.setToWmAreaCode(target.getToWmAreaCode());
                wmMoveTypeDTO.setToWmAreaName(target.getToWmAreaName());
                wmMoveTypeDTO.setFromNegativeFlag(DomainUtil.convertFlag(target.getFromNegativeFlag()));
                wmMoveTypeDTO.setToNegativeFlag(DomainUtil.convertFlag(target.getToNegativeFlag()));
                dtos.add(wmMoveTypeDTO);
            });
        } catch (Exception e) {
            LOGGER.error("convertDto.error", e);
        }
        return dtos;
    }
}
